package com.xxx;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class ManagementFunction {

    ArrayList<Student> student = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    Student st = new Student();//当前对象
    String s; //接收键入数据

    public void main(){
        File f = new File("src/student.txt");
        if(!f.exists()){
            try (
                    ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("src/student.txt"));
            ){
               oos.writeObject(student);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/student.txt"));
        ) {

            student = (ArrayList<Student>) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();

        }
        while (true) {
            System.out.println("--------欢迎使用学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 校内QQ通讯");
            System.out.println("6 退出");

            System.out.println("请输入您的选择：");
            s = sc.next();
            switch (s){
                case "1":
                    addStudent();
                    break;
                case "2":
                    delete();
                    break;
                case "3":
                    modify();
                    break;
                case "4":
                    check();
                    System.out.println("输入任意值返回主菜单~~");
                    sc.next();
                    break;
                case "5":
                    QQ();
                    System.out.println("输入任意值返回主菜单~~");
                    sc.next();
                    break;
                case "6":
                    save();
                    return;
                default:
                    System.out.println("没有该选项，请重新输入~~");
            }
        }
    }


    //自动保存
    public void save(){
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/student.txt"))
        ) {
            oos.writeObject(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加功能
    public void addStudent(){

        while (true) {
            boolean a = true;
            Student stu = new Student();
            while (a) {
                System.out.println("请输入要添加的学生id：");
                s = sc.next();

                if (student.size()==0){
                    break;
                }
                for (int i = 0; i < student.size(); i++) {
                    if (student.get(i).getId().equals(s)){
                        a = true;
                        System.out.println("id已被占用，请重新输入~~");
                        break;
                    }
                    else {
                        a = false;
                    }
                }

            }
            stu.setId(s);
            System.out.println("请输入要添加的学生姓名：");
            s = sc.next();
            stu.setName(s);
            System.out.println("请输入要添加的学生年龄：");
            s = sc.next();
            stu.setAge(s);
            System.out.println("请输入要添加的学生家庭住址：");
            s = sc.next();
            stu.setAddress(s);
            student.add(stu);
            System.out.println("请输入要添加的学生QQ号码：");
            s = sc.next();
            stu.setQQ(s);
            System.out.println("请输入要添加的学生电话号码：");
            s = sc.next();
            stu.setPhone(s);
            System.out.println("添加成功，是否继续添加？ y/n :");
            s = sc.next();
            switch (s){
                case "y":
                    save();
                    break;
                default:
                    save();
                    return;
            }
        }
    }

    //删除
    public void delete(){
        if(!check()){
            return;
        }
        System.out.println("请输入要删除的学生id：");
        s = sc.next();
        for (int i = 0; i < student.size(); i++) {
            if(student.get(i).getId().equals(s)){
                student.remove(i);
                System.out.println("删除成功");
                save();
                return;
            }
        }
        System.out.println("id不存在。");

    }

    //修改
    public void modify(){
        if(!check()){
            return;
        }
        System.out.println("请输入要修改信息的学生id：");
        s = sc.next();
        for (int i = 0; i < student.size(); i++) {
            if(student.get(i).getId().equals(s)){
                System.out.println("请输入要修改后的学生姓名：");
                s = sc.next();
                student.get(i).setName(s);
                System.out.println("请输入要修改后的学生年龄：");
                s = sc.next();
                student.get(i).setAge(s);
                System.out.println("请输入要修改后的学生家庭住址：");
                s = sc.next();
                student.get(i).setAddress(s);
                System.out.println("请输入要修改后的学生QQ号码：");
                s = sc.next();
                student.get(i).setQQ(s);
                System.out.println("请输入要修改后的电话号码：");
                s = sc.next();
                student.get(i).setPhone(s);
                System.out.println("修改成功");
                save();
                return;
            }
        }
        System.out.println("id不存在~~");

    }

    //查看
    public boolean check(){
        if(student.size()==0){
            System.out.println("当前无学生信息，请添加后操作~~");
            return false;
        }
        System.out.println("\tid\t\t\t姓名\t\t\t年龄\t\t\t家庭住址");

        for (int i = 0; i < student.size(); i++) {
            st = student.get(i);
            System.out.println(String.format("%-11.11s\t%-11.11s%-10s\t%s",st.getId(),st.getName(),st.getAge(),st.getAddress()));
        }
        return true;
    }
    //校内QQ通讯
    public void QQ(){
        if(student.size()==0){
            System.out.println("当前无学生信息，请添加后操作~~");
            return;
        }
        System.out.println("\t姓名\t\t\tQQ号\t\t\t电话号码");
        for (int i = 0; i < student.size(); i++) {
            st = student.get(i);
            System.out.println(String.format("\t%-8.8s\t%-11.11s\t\t%-10s",st.getName(),st.getQQ(),st.getPhone()));
        }
    }
}
